package com.home.customtest.typehandle;

import com.alibaba.fastjson.JSON;
import com.custom.jdbc.handler.AbstractTypeHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author XiaoBai
 * @since 2024/4/7 15:08
 */
public class JsonTypeHandler extends AbstractTypeHandler<List<TestTypeMappedHandler>> {
    @Override
    public List<TestTypeMappedHandler> getTypeValue(Object val) {
        return JSON.parseArray(val.toString(), TestTypeMappedHandler.class);
    }

    @Override
    public List<TestTypeMappedHandler> getTypeValue(ResultSet rs, int index) throws SQLException {
        return getTypeValue(rs.getObject(index));
    }

    @Override
    public void setTypeValue(PreparedStatement statement, int index, List<TestTypeMappedHandler> val) throws SQLException {
        statement.setString(index, JSON.toJSONString(val));
    }
}
